package edu.testdemo.service.impl;

import edu.nf.beans.annotations.Component;
import edu.nf.beans.annotations.Inject;
import edu.testdemo.dao.UsersDao;
import edu.testdemo.exception.LoginException;
import edu.testdemo.model.Users;
import edu.testdemo.service.LoginService;

import java.util.List;
import java.util.Map;

@Component("loginService")
public class LoginServiceImpl implements LoginService{

    @Inject(name = "userDao")
    private UsersDao userDao;

    @Override
    public Users login(Users user) {
        user = userDao.findUser(user.getUserName(), user.getPassword());
        if(user == null){
            throw new LoginException("用户名或密码错误.");
        }
        return user;
    }

    @Override
    public List<Map<String,Object>> getAllUsers() {
        List<Map<String, Object>> allUsers = userDao.getAllUsers();
        if (allUsers.size()==0){
            throw new LoginException("获取所有用户失败");
        }
        return allUsers;
    }

    @Override
    public void deleteUsers(int uid) {
        try{
            userDao.deleteUsers(uid);
        }catch (Exception e){
            throw new LoginException("删除失败");
        }
    }

    @Override
    public void updateUsers(int uid) {
        try{
            userDao.updateUsers(uid);
        }catch (Exception e){
            throw new LoginException("删除失败");
        }
    }

}
